package com.noahplat.sms.service.impl;

import com.noahplat.common.utils.*;
import com.noahplat.sms.feign.OtherSmsFeign;
import com.noahplat.sms.mapper.SmsMapper;
import com.noahplat.sms.pojo.Sms;
import com.noahplat.sms.service.SmsService;
import com.noahplat.user.feign.UserFeign;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

/**
 * @ClassName: SmsServiceImpl
 * @Description: 短信发送Service实现类
 * @Author  李景星
 * @Date 2021/2/25
 * @Version 1.0
 */
@Service
@GlobalTransactional
public class  SmsServiceImpl implements SmsService {

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private OtherSmsFeign otherSmsFeign;//注入第三方短信平台远程调用feign
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SmsMapper smsMapper;


    /**
     * 发送注册验证码短信
     * @param phone
     * @return
     */
    @Override
    public Result sendRegCode(String phone) {
        //生成短信验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);
        String code = String.format("%06d", randomNum);
        //设置短信内容
        String content = "【诺亚卫士】您的验证码"+code+"，您正在申请注册诺亚卫士账号，该验证码10分钟内有效，请勿泄露于他人。";
        //向redis中存数据
        boolean result = redisUtils.set(NoahEnum.REGCODEDIR.getCode() + phone, code, 600);
        if(!result){
            return new Result(false, StatusCode.REDISERROR, "验证码存储redis失败");
        }
        //发送短信验证码
        String username = "sdzkryyzm";
        String password = "095DEF2EE50E54D202AB57FEDD306747";
        String str = username+phone+"5744ssd270020";
        String sign = MD5Utils.getMD5SMS(str);
        String sendSms = otherSmsFeign.otherSmsSend("sdzkryyzm", password, phone, content, "5744", sign);

        if(!"0".equals((sendSms.split("&")[0]).split("=")[1])){
            return new Result(false, StatusCode.SMSERROR, "短信发送失败");
        }
        //登记短信发送流水
        Sms sms = new Sms(phone, content, new Date());
        smsMapper.insertSelective(sms);
        return new Result(true, StatusCode.OK, "短信发送成功");
    }

    /**
     * 找回密码时发送短信验证码
     * @name 李景星
     * @param phone
     * @return
     */
    @Override
    public Result sendAPassCode(Long stid,String phone) {
        Result res = userFeign.getUserByPhone(stid,phone);
        if(!res.isFlag()){
            return new Result(false,StatusCode.NOTFOUNDERROR,"该手机号没有注册");
        }

        //生成短信验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);
        String code = String.format("%06d", randomNum);
        //设置短信内容
        String content = "【诺亚卫士】您的验证码"+code+"，您正在进行找回密码操作，该验证码10分钟内有效，请勿泄露于他人。";
        //向redis中存数据
        boolean result = redisUtils.set(NoahEnum.FINDCODEDIR.getCode() + phone, code, 600);
        if(!result){
            return new Result(false, StatusCode.REDISERROR, "验证码存储redis失败");
        }
        //发送短信验证码
        String username = "sdzkryyzm";
        String password = "095DEF2EE50E54D202AB57FEDD306747";
        String str = username+phone+"5744ssd270020";
        String sign = MD5Utils.getMD5SMS(str);
        String sendSms = otherSmsFeign.otherSmsSend("sdzkryyzm", password, phone, content, "5744", sign);

        if(!"0".equals((sendSms.split("&")[0]).split("=")[1])){
            return new Result(false, StatusCode.SMSERROR, "短信发送失败");
        }
        //登记短信发送流水
        Sms sms = new Sms(phone, content, new Date());
        smsMapper.insertSelective(sms);

        return new Result(true, StatusCode.OK, "短信发送成功");
    }

    /**
     * 修改手机号发送短信验证码
     * @name 李景星
     * @param phone
     * @return
     */
    @Override
    public Result sendUpdateCode(String phone) {
        //生成短信验证码
        Random random = new Random();
        int randomNum = random.nextInt(1000000);
        String code = String.format("%06d", randomNum);
        //设置短信内容
        String content = "【诺亚卫士】您的验证码"+code+"，您正在进行修改手机号操作，该验证码10分钟内有效，请勿泄露于他人。";
        //向redis中存数据
        boolean result = redisUtils.set(NoahEnum.UPDATECODEDIR.getCode() + phone, code, 600);
        if(!result){
            return new Result(false, StatusCode.REDISERROR, "验证码存储redis失败");
        }
        //发送短信验证码
        String username = "sdzkryyzm";
        String password = "095DEF2EE50E54D202AB57FEDD306747";
        String str = username+phone+"5744ssd270020";
        String sign = MD5Utils.getMD5SMS(str);
        String sendSms = otherSmsFeign.otherSmsSend("sdzkryyzm", password, phone, content, "5744", sign);

        if(!"0".equals((sendSms.split("&")[0]).split("=")[1])){
            return new Result(false, StatusCode.SMSERROR, "短信发送失败");
        }
        //登记短信发送流水
        Sms sms = new Sms(phone, content, new Date());
        smsMapper.insertSelective(sms);

        return new Result(true, StatusCode.OK, "短信发送成功");
    }
}
